package com.ecoolex.chinadutyfree.retailer.core.manager;

import com.ecoolex.chinadutyfree.retailer.core.dao.DirectMailGoodsOrderSenderDAO;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrder;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderSender;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderSender.Column;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderSenderExample;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.user.system.entity.UserSender;
import com.ecoolex.user.system.ws.UserAddressService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;

@Service
public class DirectMailGoodsOrderSenderManager {
    @Autowired
    private DirectMailGoodsOrderSenderDAO dao;


    @Reference
    private UserAddressService userAddressService;

    public DirectMailGoodsOrderSender builder(Integer senderId, Integer userId) {
        if (Check.isNull(senderId)) {
            return null;
        }

        UserSender userSender = userAddressService.getUserSenderByIdAndUserId(senderId, userId);
        BizAssert.isTrue(Check.notNull(userSender), ResultCode.VERIFY.build(10), "寄件人地址不存在");

        DirectMailGoodsOrderSender sender = new DirectMailGoodsOrderSender();
        sender.setSender(userSender.getSender());
        sender.setPhone(userSender.getPhone());
        return sender;
    }

    public DirectMailGoodsOrderSender add(DirectMailGoodsOrder order, DirectMailGoodsOrderSender sender, String createUser) {
        sender.setOrderId(order.getOrderId());
        sender.setDeleted(DirectMailGoodsOrderSender.NOT_DELETED);
        sender.setCreateUser(createUser);
        sender.setCreateTime(Date.from(Instant.now()));
        sender.setUpdateUser(createUser);
        sender.setUpdateTime(Date.from(Instant.now()));
        dao.insert(sender);
        return sender;
    }

    public DirectMailGoodsOrderSender getByOrderId(Integer orderId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(orderId, DirectMailGoodsOrderSender.NOT_DELETED);
    }
    
    public DirectMailGoodsOrderSender getPossibleBeDeletedByOrderId(Integer orderId) {
        return dao.selectByPrimaryKey(orderId);
    }

    public void deleteByOrderId(DirectMailGoodsOrder order, String updateUser) {
        DirectMailGoodsOrderSender record = new DirectMailGoodsOrderSender();
        record.setDeleted(DirectMailGoodsOrderSender.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DirectMailGoodsOrderSenderExample example = new DirectMailGoodsOrderSenderExample();
        example.createCriteria().andOrderIdEqualTo(order.getOrderId()).andDeletedEqualTo(
                DirectMailGoodsOrderSender.NOT_DELETED);

        dao.updateByExampleSelective(record, example, Column.deleted, Column.updateUser, Column.updateTime);
    }

    public DirectMailGoodsOrderSender updateSender(DirectMailGoodsOrderSender sender,String updateUser) {
        sender.setUpdateUser(updateUser);
        sender.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(sender, Column.excludes(Column.deleted,Column.createTime,Column.createUser));
        return sender;
    }

    public DirectMailGoodsOrderSender addSender(DirectMailGoodsOrderSender sender, String createUser) {
        sender.setDeleted(DirectMailGoodsOrderSender.NOT_DELETED);
        sender.setCreateUser(createUser);
        sender.setCreateTime(Date.from(Instant.now()));
        sender.setUpdateUser(createUser);
        sender.setUpdateTime(Date.from(Instant.now()));
        dao.insert(sender);
        return sender;
    }

    public void deleteSender(DirectMailGoodsOrderSender sender, String updateUser) {
        dao.deleteByPrimaryKey(sender.getOrderId());
    }
}